package cn.nexd.map.rendering.core.componet.map;

import android.graphics.Picture;
import android.graphics.RectF;
import android.graphics.drawable.PictureDrawable;
import android.util.Log;

import java.util.List;
import java.util.Map;

import cn.nexd.map.rendering.core.componet.data.Feature;
import cn.nexd.map.rendering.core.componet.data.FloorsFeature;



/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
 * applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

/**
 * Describes a vector Picture object, and optionally its bounds.
 *
 * @author Larva Labs, LLC
 */
public class SVG {

    /**
     * The parsed Picture object.
     */
    private final Picture picture;
    private SVGParser.SVGHandler handler;

    /**
     * These are the bounds for the SVG specified as a hidden "bounds" layer in the SVG.
     */
    private final RectF bounds;

    /**
     * These are the estimated bounds of the SVG computed from the SVG elements while parsing. Note that this could be
     * null if there was a failure to compute limits (ie. an empty SVG).
     */
    private RectF limits = null;

    private PictureDrawable drawable = null;

    /**
     * Construct a new SVG.
     *
     * @param picture the parsed picture object.
     * @param handler
     */
    SVG(Picture picture, SVGParser.SVGHandler handler) {
        this.picture = picture;
        this.handler = handler;
        this.bounds = handler.bounds;
    }

    /**
     * Set the limits of the SVG, which are the estimated bounds computed by the parser.
     *
     * @param limits the bounds computed while parsing the SVG, may not be entirely accurate.
     */
    void setLimits(RectF limits) {
        this.limits = limits;
    }

    /**
     * Get a {@link android.graphics.drawable.Drawable} of the SVG.
     *
     * @return the PictureDrawable.
     */
    public PictureDrawable getDrawable() {
        if (drawable == null) {
            drawable = new PictureDrawable(picture);
        }
        return drawable;
    }

    /**
     * Get the parsed SVG picture data.
     *
     * @return the picture.
     */
    public Picture getPicture() {

        //测试SVG文件对象化
        FloorsFeature floorsFeature = handler.getFloorsFeature();
        Log.d("floorfeature", "location: " + floorsFeature.getLocation() + " floor: " + floorsFeature.getFloor() + " north: " + floorsFeature.getNorth());

        List<Map<Long, Feature>> features = floorsFeature.getFeatures();

        for (int i = 0; i < features.size(); i++) {
            Map<Long, Feature> featureMap = features.get(i);

            for (Map.Entry<Long, Feature> entry : featureMap.entrySet()) {
                Log.d("floorfeature", "\nid :" + entry.getKey() + " " + entry.getValue().getFeature_id() + " \nshape: " + entry.getValue().getShape_id()

                        + " \ntype: " + entry.getValue().getType_id()
                        + " \nname: " + entry.getValue().getName()
                        + "\nurl: " + entry.getValue().getUrl()
                        + "\ncenter-x: " + entry.getValue().getCenter_x()
                        + "\ncenter-y: " + entry.getValue().getCenter_y()
                        + " \npath: " + entry.getValue().getPath().toString());
            }
        }

        ///
        return picture;
    }

    /**
     * Gets the bounding rectangle for the SVG, if one was specified.
     *
     * @return rectangle representing the bounds.
     */
    public RectF getBounds() {
        return bounds;
    }

    /**
     * Gets the bounding rectangle for the SVG that was computed upon parsing. It may not be entirely accurate for
     * certain curves or transformations, but is often better than nothing.
     *
     * @return rectangle representing the computed bounds.
     */
    public RectF getLimits() {
        return limits;
    }

    /**
     * 返回FloorsFeature
     */
    public FloorsFeature getFloorsFeature() {
        return handler.getFloorsFeature();
    }
}
